﻿using System;
using System.Collections.Generic;
using System.Text;

using Newtonsoft.Json;


    /// <summary>
    /// 给定的需要直接调用tostring方法，输出原生json的类
    /// </summary>
    public class RawJsonStringConverter : JsonConverter
    {
        private Type _specialType;

        /// <summary>
        /// 
        /// </summary>
        public Type SpecialType
        {
            get { return _specialType; }
            set { _specialType = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public RawJsonStringConverter(Type value)
        {
            _specialType = value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="value"></param>
        /// <param name="serializer"></param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteRawValue(value.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="objectType"></param>
        /// <param name="existingValue"></param>
        /// <param name="serializer"></param>
        /// <returns></returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return null;
        }

        /// <summary>
        /// 转换的时候，先得到 和 要转换的类型 匹配的Converter （GetMatchingConverter方法），调用Converter.CanConvert,如果true，则返回该Converter
        /// </summary>
        /// <param name="objectType"></param>
        /// <returns></returns>
        public override bool CanConvert(Type objectType)
        {
            if (objectType == _specialType)
                return true;
            return false;
        }
    }
